package com.zhugang.week03;

/**
 * @program algorithms
 * @description: arrayStack
 * @author: chanzhugang
 * @create: 2022/06/01 18:52
 */
public class ArrayStack {

    private final String[] items;
    private int count;
    private final int n;

    public ArrayStack(String[] items, int count, int n) {
        this.items = items;
        this.count = count;
        this.n = n;
    }

    /**
     * 入栈
     *
     * @param item
     * @return
     */
    public boolean push(String item) {
        if (count == n) {
            // 空间不够，入栈失败
            return false;
        }
        items[count] = item;
        count++;
        return true;
    }

    /**
     * 出栈
     *
     * @return¶
     */
    public String pop() {
        if (count == 0) {
            // 栈为空
            return null;
        }
        String item = items[count - 1];
        count--;
        return item;
    }

    /**
     * 读顶元素
     *
     * @return
     */
    public String peek() {
        if (count == 0) {
            return null;
        }
        return items[count - 1];
    }

}